home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue47 / OOPRules / frm2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-05-30  |  1.9 KB  |  83 lines

  1. unit frm2;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TFormDialog = class(TForm)
  11.     procedure FormCreate(Sender: TObject);
  12.     procedure btnAddClick(Sender: TObject);
  13.   private
  14.     Edit1: TEdit;
  15.     ListItems: TListBox;
  16.     btnAdd: TButton;
  17.     function GetItems(Index: Integer): string;
  18.     procedure SetItems(Index: Integer; const Value: string);
  19.   protected
  20.     function GetText: String; virtual;
  21.     procedure SetText(const Value: String); virtual;
  22.   public
  23.     constructor Create (Text: string); reintroduce; overload;
  24.     property Text: String
  25.      read GetText write SetText;
  26.     property Items [Index: Integer]: string
  27.       read GetItems write SetItems; default;
  28.   end;
  29.  
  30. implementation
  31.  
  32. {$R *.DFM}
  33.  
  34. { TForm2 }
  35.  
  36. constructor TFormDialog.Create(Text: string);
  37. begin
  38.   inherited Create (Application);
  39.   Edit1.Text := Text;
  40. end;
  41.  
  42. function TFormDialog.GetText: String;
  43. begin
  44.   Result := Edit1.Text;
  45. end;
  46.  
  47. procedure TFormDialog.SetText(const Value: String);
  48. begin
  49.   Edit1.Text := Value;
  50. end;
  51.  
  52. procedure TFormDialog.FormCreate(Sender: TObject);
  53. begin
  54.   Edit1 := FindComponent ('Edit1') as TEdit;
  55.   ListItems := FindComponent ('ListItems') as TListBox;
  56.   btnAdd := FindComponent ('btnAdd') as TButton;
  57. end;
  58.  
  59. function TFormDialog.GetItems(Index: Integer): string;
  60. begin
  61.   if Index >= ListItems.Items.Count then
  62.     raise Exception.Create ('TFormDialog: Out of Range');
  63.   Result := ListItems.Items [Index];
  64. end;
  65.  
  66. procedure TFormDialog.SetItems(Index: Integer; const Value: string);
  67. begin
  68.   if Index >= ListItems.Items.Count then
  69.     raise Exception.Create ('TFormDialog: Out of Range');
  70.   ListItems.Items [Index] := Value;
  71. end;
  72.  
  73. procedure TFormDialog.btnAddClick(Sender: TObject);
  74. begin
  75.   ListItems.Items.Add (Edit1.Text);
  76. end;
  77.  
  78. initialization
  79.   RegisterClasses ([TEdit,
  80.     TListBox,
  81.     TButton]);
  82. end.
  83.